package cn.lingyangwl.agile.model.module.tenant;

import cn.lingyangwl.agile.model.module.auth.*;
import cn.lingyangwl.framework.tool.core.*;
import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.Getter;
import lombok.Setter;

import java.util.Objects;

/**
 * 工作空间上下文
 * @author shenguangyang
 */

public class TenantContext {
    public static final String EMPTY_TENANT_ID = "-1";
    private static final TransmittableThreadLocal<TenantContext> TTL = new TransmittableThreadLocal<>();

    public static void set(TenantContext content) {
        TTL.set(content);
    }

    public static TenantContext get() {
        TenantContext tenantContext = TTL.get();
        if (Objects.isNull(tenantContext)) {
            tenantContext = new TenantContext();
            TTL.set(tenantContext);
        }
        return tenantContext;
    }

    public static void clear() {
        TTL.remove();
    }

    /**
     * 当前租户id
     */
    private String tenantId;

    /**
     * 原始客户端请求的租户id, {@link #tenantId} 在业务处理过程中可能会被临时修改成其他的租户id, 但是在作用的代码块中,
     * 可能还要获取客户端传递的租户id, 就可以直接从该字段获取
     */
    @Getter
    private String rawTenantId;

    /**
     * 主租户id
     */
    @Getter
    private String mainTenantId = EMPTY_TENANT_ID;

    /**
     * 是否为管理租户类型(用于管理某一个场景)
     */
    @Getter
    private Boolean isMgtScene = Boolean.FALSE;


    @Getter
    private boolean isFilterTenant = true;

    /**
     * 是否过滤主租户
     */
    @Getter
    private boolean isFilterMainTenant = true;

    /**
     * 管理场景的租户id
     */
    @Getter
    @Setter
    private String mgtSceneTenantId = EMPTY_TENANT_ID;

    public TenantContext filterTenant(boolean filterTenant) {
        isFilterTenant = filterTenant;
        return this;
    }

    public TenantContext filterMainTenant(boolean isFilter) {
        isFilterMainTenant = isFilter;
        return this;
    }

    public TenantContext setTenantId(String tenantId) {
        this.tenantId = isValidTenantId(tenantId) ? tenantId : "";
        this.tenantId = StringUtils.defaultIfEmpty(this.tenantId, "-1");
        return this;
    }

    public TenantContext setRawTenantId(String rawTenantId) {
        this.rawTenantId = isValidTenantId(rawTenantId) ? rawTenantId : "";
        this.rawTenantId = StringUtils.defaultIfEmpty(this.rawTenantId, "-1");
        return this;
    }

    public static boolean isValidTenantId(String tenantId) {
        return !(StringUtils.isEmpty(tenantId) || "null".equals(tenantId) || "undefined".equals(tenantId) || "-1".equals(tenantId));
    }

    public void setMainTenantId(String mainTenantId) {
        this.mainTenantId = isValidTenantId(mainTenantId) ? mainTenantId : "";
        this.mainTenantId = StringUtils.defaultIfEmpty(this.mainTenantId, "-1");
    }

    public TenantContext setTenantId(Long tenantId) {
        this.tenantId = String.valueOf(tenantId);
        return this;
    }

    public void setTenant(AuthUserTenant tenant) {
        if (Objects.isNull(tenant)) {
            return;
        }
        this.tenantId = tenant.getTenantId();
        this.rawTenantId = tenant.getTenantId();
        this.mainTenantId = tenant.getMainTenantId();
        this.mgtSceneTenantId = tenant.getMgtSceneTenantId();
        this.isMgtScene = tenant.getIsMgtScene();
    }

    /**
     * 是否为管理场景租户
     */
    public boolean isMgtSceneTenant() {
        return Objects.equals(this.mgtSceneTenantId, this.getTenantId());
    }

    public String getTenantId() {
        return tenantId;
    }

    public boolean isMainTenant() {
        return Objects.equals(this.tenantId, this.mainTenantId);
    }
}
